package com.fingard.dsp.bank.dbobj.receipt;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.fingard.FGBiz;
import com.fingard.constant.Format;
import com.fingard.db.JDBCHelper;
import com.fingard.dsp.bank.dbobj.unique.UniqueIdHelper;
import com.fingard.dsp.bank.helpers.dbhelpers.InsertHelper;
import com.fingard.dsp.bank.helpers.dbhelpers.LoadHelper;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;

/**回单数据操作类*/
public class ReceiptDataHelper {
	private static final int maxTabCount = 10;
	
	public static int getMaxTabCount(){
		return maxTabCount;
	}
	
	private Connection partDbconn = null;
	private String tenant = "";
	
	/**！！注意：需在finally里显示调用close()方法
	 * @param pTenant 租户
	 * @param pPartDate 日期*/
	public ReceiptDataHelper(String pTenant, Date pPartDate) throws Exception{
		tenant = pTenant;
		partDbconn = FGBiz.partTable.getLoaclDbConnByPart(pTenant, pPartDate);
	}
	
	public String getTenant(){
		return tenant;
	}
	
	private static String getTableName(String pActNum){
		int sum = 0;
		for(int i=0;i<pActNum.length();i++){
			sum += pActNum.charAt(i);
		}
		return "RECEIPTDATA" + (sum%maxTabCount);
	}
	
	public static int getTableIdxByAct(String pActNum){
		int sum = 0;
		for(int i=0;i<pActNum.length();i++){
			sum += pActNum.charAt(i);
		}
		return sum%maxTabCount;
	}
	
	private static String getTabNameByIdx(int pIdx){
		return "RECEIPTDATA" + pIdx;
	}
	
	/**判断是否存在RECEIPTDATA0，0接口，用于判断是否升级了电子回单逻辑*/
	public boolean existTable() throws SQLException{
		return FGBiz.partTable.isExistTable(partDbconn, "RECEIPTDATA0");
	}
	
	public void close(){
		if(partDbconn != null){
			try{
				partDbconn.close();
			}catch(Exception ex){
				FGBiz.limitMsg.exception("ReceiptDataHelper.closePartDbConn", ex);
			}
		}
		if(localConn!=null){
			try{
				localConn.close();
			}catch(Exception ex){
				FGBiz.limitMsg.exception("ReceiptDataHelper.closeLocalDbConn", ex);
			}
		}
	}
	
	/**将回单数据写入数据库表*/
	public int insertIfNotExist(ReceiptData pData) throws Exception{
		return insertIfNotExist(pData, false);
	}
	
	private Connection localConn = null;
	
	private Connection getLocalDbConn() throws Exception{
		if(localConn == null){
			localConn = FGBiz.getLocalDbConn();
		}
		return localConn;
	}
	
	/**将回单数据写入数据库表 pRetZeroIfExist 已存在返回0
	 * @param pRetZeroIfExist，已存在返回0，新增大于0*/
	public int insertIfNotExistByReceiptNo(ReceiptData pData, boolean pRetZeroIfExist) throws Exception{
		String tabName = getTableName(pData.ownAct);
		String chkSql = "select count(*) from "+tabName+" where RECEIPTNO=? and OWNACT=? and BUSDATE=?";
		Object[] objs = new Object[3];
		objs[0] = pData.receiptNo;
		objs[1] = pData.ownAct;
		objs[2] = pData.busDate;
		Object chkObj = JDBCHelper.execScalar(partDbconn, chkSql, objs);
		int chkCount = -1;
		if(chkObj != null){
			chkCount = Integer.parseInt(chkObj.toString());
		}
		if(chkCount <= 0){
			pData.urid = UniqueIdHelper.getUniqueId(getLocalDbConn(), "RECEIPTDATA");
			pData.createdOn = new Date();
			pData.lastModifiedOn = new Date();
			pData.rowVersion = 1;
			int rowAff = InsertHelper.insertObject(partDbconn, tabName, pData);
			if(rowAff == 1){
				return 1;
			}else{
				throw new Exception("写入"+tabName+"的影响行数："+rowAff);
			}
		}else{
			if(pRetZeroIfExist){
				return 0;
			}
			return chkCount;
		}
	}
	
	/**将回单数据写入数据库表 pRetZeroIfExist 已存在返回0
	 * @param pRetZeroIfExist，已存在返回0，新增大于0*/
	public int insertIfNotExist(ReceiptData pData, boolean pRetZeroIfExist) throws Exception{
		String tabName = getTableName(pData.ownAct);
		String chkSql = "select count(*) from "+tabName+" where ISTNBR=? and OWNACT=? and BUSDATE=?";
		Object[] objs = new Object[3];
		objs[0] = pData.istNbr;
		objs[1] = pData.ownAct;
		objs[2] = pData.busDate;
		Object chkObj = JDBCHelper.execScalar(partDbconn, chkSql, objs);
		int chkCount = -1;
		if(chkObj != null){
			chkCount = Integer.parseInt(chkObj.toString());
		}
		if(chkCount <= 0){
			pData.urid = UniqueIdHelper.getUniqueId(getLocalDbConn(), "RECEIPTDATA");
			pData.createdOn = new Date();
			pData.lastModifiedOn = new Date();
			pData.rowVersion = 1;
			int rowAff = InsertHelper.insertObject(partDbconn, tabName, pData);
			if(rowAff == 1){
				return 1;
			}else{
				throw new Exception("写入"+tabName+"的影响行数："+rowAff);
			}
		}else{
			if(pRetZeroIfExist){
				return 0;
			}
			return chkCount;
		}
	}
	
	/**更新BUSREF*/
	public int updateBusRef(ReceiptData pData) throws Exception{
		String tabName = getTableName(pData.ownAct);
		String updSql = "update "+tabName+" set BUSREF=?,LASTMODIFIEDON=?,ROWVERSION=ROWVERSION+1 where ISTNBR=? and OWNACT=?";
		Object[] objs = new Object[4];
		objs[0] = pData.busRef;
		objs[1] = new Date();
		objs[2] = pData.istNbr;
		objs[3] = pData.ownAct;
		return JDBCHelper.update(partDbconn, updSql, objs);
	}
	
	/**获取需要推送（push）的URID列表，PUSHSTATE in('1','3') and UPSTATE='2'
	 * @param pRetryMins 推送中，补偿推送的最小间隔分钟数
	 * */
	public ArrayList<Long> getNeedPushList(int pTabIdx, Date pDate, int pRetryMins) throws SQLException{
		String tabName = getTabNameByIdx(pTabIdx);
		ArrayList<Long> list = new ArrayList<Long>();
		int busDate = Integer.parseInt(Format.Date8Format.format(pDate));
		Date updPushTime = new Date();
		String updSql = "update "+tabName+" set PUSHSTATE='4',PUSHINFO='',PUSHTIME=?,LASTMODIFIEDON=?,ROWVERSION=ROWVERSION+1 where BUSDATE=? and UPSTATE='2' and (PUSHSTATE in('1','3') or (PUSHSTATE='4' and PUSHTIME<?))";

		Object[] updObjs = new Object[4];
		updObjs[0] = updPushTime;
		updObjs[1] = updPushTime;
		updObjs[2] = busDate;
		updObjs[3] = DateHelper.addMinutes(updPushTime, -pRetryMins);//长时间还处于推送中的
		int updRows = JDBCHelper.update(partDbconn, updSql, updObjs);
		if(updRows<=0){
			return list;
		}
		String sql = "select URID from "+tabName+" where BUSDATE=? and UPSTATE='2' and PUSHSTATE='4' and PUSHTIME=?";
		Object[] objs = new Object[2];
		objs[0] = busDate;
		objs[1] = updPushTime;
		List<Map<String, Object>> mapList = JDBCHelper.execQuery(partDbconn, sql, objs);
		if (mapList != null && mapList.size() > 0) {
			for (int i = 0; i < mapList.size(); i++) {
	            Map<String, Object> eachMap = mapList.get(i);
	            list.add(Long.parseLong(eachMap.get("URID").toString()));
	        }
		}
		return list;
	}
	
	/**获取已经推送（push）的总记录数
	 * @throws SQLException */
	public int getPushedCount(int pTabIdx, Date pDate) throws SQLException{
		String tabName = getTabNameByIdx(pTabIdx);
		int busDate = Integer.parseInt(Format.Date8Format.format(pDate));
		String sql = "select count(*) from "+tabName+" where BUSDATE=? and PUSHSTATE='2'";
		Object[] objs = new Object[1];
		objs[0] = busDate;
		Object rdCount = JDBCHelper.execScalar(partDbconn, sql, objs);
		return Integer.parseInt(rdCount.toString());
	}
	
	
	/**获取需要上传的URID列表，UPSTATE in('1','3') and DOWNSTATE='2'*/
	public ArrayList<Long> getNeedUpList(int pTabIdx, Date pDate) throws SQLException{
		String tabName = getTabNameByIdx(pTabIdx);
		ArrayList<Long> list = new ArrayList<Long>();
		int busDate = Integer.parseInt(Format.Date8Format.format(pDate));
		String sql = "select URID from "+tabName+" where BUSDATE=? and UPSTATE in('1','3') and DOWNSTATE='2'";
		Object[] objs = new Object[1];
		objs[0] = busDate;
		List<Map<String, Object>> mapList = JDBCHelper.execQuery(partDbconn, sql, objs);
		if (mapList != null && mapList.size() > 0) {
			for (int i = 0; i < mapList.size(); i++) {
	            Map<String, Object> eachMap = mapList.get(i);
	            list.add(Long.parseLong(eachMap.get("URID").toString()));
	        }
		}
		return list;
	}
	
	/**获取已经上传的总记录数
	 * @throws SQLException */
	public int getUpedCount(int pTabIdx, Date pDate) throws SQLException{
		String tabName = getTabNameByIdx(pTabIdx);
		int busDate = Integer.parseInt(Format.Date8Format.format(pDate));
		String sql = "select count(*) from "+tabName+" where BUSDATE=? and UPSTATE='2'";
		Object[] objs = new Object[1];
		objs[0] = busDate;
		Object rdCount = JDBCHelper.execScalar(partDbconn, sql, objs);
		return Integer.parseInt(rdCount.toString());
	}
	
	/**获取总记录数
	 * @throws SQLException */
	public int getTotalCountByAct(String pActNum, Date pDate) throws SQLException{
		String tabName = getTableName(pActNum);
		int busDate = Integer.parseInt(Format.Date8Format.format(pDate));
		String sql = "select count(*) from "+tabName+" where BUSDATE=? and OWNACT=?";
		Object[] objs = new Object[2];
		objs[0] = busDate;
		objs[1] = pActNum;
		Object rdCount = JDBCHelper.execScalar(partDbconn, sql, objs);
		return Integer.parseInt(rdCount.toString());
	}
	
	/**获取当天新增的记录数
	 * @throws SQLException 
	 * @throws ParseException */
	public int countCreatedOnToday(String pActNum, Date pDate) throws SQLException, ParseException{
		String tabName = getTableName(pActNum);
		int busDate = Integer.parseInt(Format.Date8Format.format(pDate));
		String sql = "select count(*) from "+tabName+" where BUSDATE=? and OWNACT=? and CREATEDON>=?";
		Object[] objs = new Object[3];
		objs[0] = busDate;
		objs[1] = pActNum;
		SimpleDateFormat dateFrmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		objs[2] = dateFrmt.parse(Format.Date10Format.format(new Date()) + " 00:00:00");
		Object rdCount = JDBCHelper.execScalar(partDbconn, sql, objs);
		return Integer.parseInt(rdCount.toString());
	}
	
	/**获取总记录数
	 * @throws SQLException */
	public int getTotalCount(int pTabIdx, Date pDate) throws SQLException{
		String tabName = getTabNameByIdx(pTabIdx);
		int busDate = Integer.parseInt(Format.Date8Format.format(pDate));
		String sql = "select count(*) from "+tabName+" where BUSDATE=?";
		Object[] objs = new Object[1];
		objs[0] = busDate;
		Object rdCount = JDBCHelper.execScalar(partDbconn, sql, objs);
		return Integer.parseInt(rdCount.toString());
	}
	
	/**获取已经下载的总记录数
	 * @throws SQLException */
	public int getDownedCount(String pActNum, Date pDate) throws SQLException{
		String tabName = getTableName(pActNum);
		return getDownedCountByTName(tabName, pDate);
	}
	
	/**获取已经下载的总记录数
	 * @throws SQLException */
	public int getDownedCount(int pTabIdx, Date pDate) throws SQLException{
		String tabName = getTabNameByIdx(pTabIdx);
		return getDownedCountByTName(tabName, pDate);
	}
	
	/**获取已经上传完成的数据
	 * @throws SQLException 
	 * @throws ParseException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException */
	public ArrayList<ReceiptData> getUploadedDataList(String pActNum, Date pDate) throws SQLException, IllegalArgumentException, IllegalAccessException, ParseException{
		String tabName = getTableName(pActNum);
		int busDate = Integer.parseInt(Format.Date8Format.format(pDate));
		String sql = "select * from "+tabName+" where BUSDATE=? and OWNACT=? and UPSTATE='2'";
		Object[] objs = new Object[2];
		objs[0] = busDate;
		objs[1] = pActNum;
		List<Map<String, Object>> mapList = JDBCHelper.execQuery(partDbconn, sql, objs);
		ArrayList<ReceiptData> retList = new ArrayList<ReceiptData>();
		if (mapList != null && mapList.size() > 0) {
			for (int i = 0; i < mapList.size(); i++) {
	            Map<String, Object> eachMap = mapList.get(i);
	            ReceiptData eachData = new ReceiptData();
				LoadHelper.loadObject(eachMap, eachData);
				retList.add(eachData);
	        }
		}
		return retList;
	}

	/**获取需要下载文件的记录
	 * @throws SQLException
	 * @throws ParseException
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException */
	public ArrayList<ReceiptData> getNeedDwnDataList(String pActNum, Date pDate) throws SQLException, IllegalArgumentException, IllegalAccessException, ParseException{
		String tabName = getTableName(pActNum);
		int busDate = Integer.parseInt(Format.Date8Format.format(pDate));
		String sql = "select * from "+tabName+" where BUSDATE=? and OWNACT=? and downstate='1'";
		Object[] objs = new Object[2];
		objs[0] = busDate;
		objs[1] = pActNum;
		List<Map<String, Object>> mapList = JDBCHelper.execQuery(partDbconn, sql, objs);
		ArrayList<ReceiptData> retList = new ArrayList<ReceiptData>();
		if (mapList != null && mapList.size() > 0) {
			for (int i = 0; i < mapList.size(); i++) {
				Map<String, Object> eachMap = mapList.get(i);
				ReceiptData eachData = new ReceiptData();
				LoadHelper.loadObject(eachMap, eachData);
				retList.add(eachData);
			}
		}
		return retList;
	}
	
	
	private int getDownedCountByTName(String pTabName, Date pDate) throws SQLException{
		int busDate = Integer.parseInt(Format.Date8Format.format(pDate));
		String sql = "select count(*) from "+pTabName+" where BUSDATE=? and DOWNSTATE='2'";
		Object[] objs = new Object[1];
		objs[0] = busDate;
		Object rdCount = JDBCHelper.execScalar(partDbconn, sql, objs);
		return Integer.parseInt(rdCount.toString());
	}
	
	public ReceiptData loadFromDbByReceiptNo(String pOwnAct, String pReceiptNo) throws SQLException, IllegalArgumentException, IllegalAccessException, ParseException{
		String tabName = getTableName(pOwnAct);
		String sql = "select * from "+tabName+" where RECEIPTNO=? and OWNACT=?";
		Object[] objs = new Object[2];
		objs[0] = pReceiptNo;
		objs[1] = pOwnAct;
		return loadOneDataBySql(sql, objs);
	}
	
	/**根据回单实例号取回单数据*/
	public ReceiptData loadFromDbByIstNbr(String pOwnAct, String pIstNbr) throws SQLException, IllegalArgumentException, IllegalAccessException, ParseException{
		String tabName = getTableName(pOwnAct);
		String sql = "select * from "+tabName+" where ISTNBR=? and OWNACT=?";
		Object[] objs = new Object[2];
		objs[0] = pIstNbr;
		objs[1] = pOwnAct;
		return loadOneDataBySql(sql, objs);
	}
	
	public ReceiptData loadFromDbByBusRef(String pOwnAct, String pBusRef) throws SQLException, IllegalArgumentException, IllegalAccessException, ParseException{
		String tabName = getTableName(pOwnAct);
		String sql = "select * from "+tabName+" where BUSREF=? and OWNACT=?";
		Object[] objs = new Object[2];
		objs[0] = pBusRef;
		objs[1] = pOwnAct;
		return loadOneDataBySql(sql, objs);
	}
	
	/**根据urid list获取ArrayList<ReceiptData>，不要超1000*/
	public ArrayList<ReceiptData> loadFromDbByUridList(int pTabIdx, ArrayList<Long> pUridList) throws SQLException, IllegalArgumentException, IllegalAccessException, ParseException{
		String tabName = getTabNameByIdx(pTabIdx);
		String sql = "select * from "+tabName+" where URID in(" + StringHelper.joinLongArray(",", pUridList) + ")";
		
		List<Map<String, Object>> mapList = JDBCHelper.execQuery(partDbconn, sql, null);
		ArrayList<ReceiptData> retList = new ArrayList<ReceiptData>();
		if (mapList != null && mapList.size() > 0) {
			for (int i = 0; i < mapList.size(); i++) {
	            Map<String, Object> eachMap = mapList.get(i);
	            ReceiptData eachData = new ReceiptData();
				LoadHelper.loadObject(eachMap, eachData);
				retList.add(eachData);
	        }
		}
		return retList;
	}
	
	public ReceiptData loadFromDbByUrid(int pTabIdx, long pUrid) throws SQLException, IllegalArgumentException, IllegalAccessException, ParseException{
		String tabName = getTabNameByIdx(pTabIdx);
		String sql = "select * from "+tabName+" where URID=?";
		Object[] objs = new Object[1];
		objs[0] = pUrid;
		return loadOneDataBySql(sql, objs);
	}
	
	/**针对利息手续费，根据金额+收付方向查找*/
	public ReceiptData loadFromDbByAmount(int pBusDate, String pOwnAct, BigDecimal pAmount, String pMoneyWay) throws Exception{
		String tabName = getTableName(pOwnAct);
		String sql = "select * from "+tabName+" where BUSDATE=? and OWNACT=? and AMOUNT=? and MONEYWAY=? and (RECEIPTTYPE like '%利息%' or RECEIPTTYPE like '%手续费%')";
		Object[] objs = new Object[4];
		objs[0] = pBusDate;
		objs[1] = pOwnAct;
		objs[2] = pAmount;
		objs[3] = pMoneyWay;
		List<Map<String, Object>> mapDataList = JDBCHelper.execQuery(partDbconn, sql, objs);
		if(mapDataList.size() == 1){
			ReceiptData respData = new ReceiptData();
			LoadHelper.loadObject(mapDataList.get(0), respData);
			return respData;
		}else if(mapDataList.size() > 1){
			throw new Exception("根据条件AMOUNT:"+pAmount+",MONEYWAY:"+pMoneyWay+"找到多条记录");
		}
		return null;
	}
	
	private ReceiptData loadOneDataBySql(String pSql, Object[] pObjPms) throws SQLException, IllegalArgumentException, IllegalAccessException, ParseException{
		Map<String,Object> mapData = JDBCHelper.execQuerySingle(partDbconn, pSql, pObjPms);
		if(mapData != null && mapData.size() > 0){
			ReceiptData respData = new ReceiptData();
			LoadHelper.loadObject(mapData, respData);
			return respData;
		}
		return null;
	}
	
	public ArrayList<String> getIstNbrList(String pOwnAct, Date pDate) throws SQLException{
		ArrayList<String> list = new ArrayList<String>();
		String tabName = getTableName(pOwnAct);
		int busDate = Integer.parseInt(Format.Date8Format.format(pDate));
		String sql = "select ISTNBR from "+tabName+" where BUSDATE=? and OWNACT=? and DOWNSTATE='1'";
		Object[] objs = new Object[2];
		objs[0] = busDate;
		objs[1] = pOwnAct;
		List<Map<String, Object>> mapList = JDBCHelper.execQuery(partDbconn, sql, objs);
		if (mapList != null && mapList.size() > 0) {
			for (int i = 0; i < mapList.size(); i++) {
	            Map<String, Object> eachMap = mapList.get(i);
	            list.add(eachMap.get("ISTNBR").toString());
	        }
		}
		return list;
	}
	
	public int updateDownState(String pOwnAct, String pIstNbr, String pRelativeFilePath, String pSrcFile, String pDownState) throws SQLException{
		String tabName = getTableName(pOwnAct);
		String sql = "update "+tabName+" set LOCALFILE=?,SRCFILE=?,DOWNSTATE=?,DOWNTIME=?,LASTMODIFIEDON=?,ROWVERSION=ROWVERSION+1 where ISTNBR=? and OWNACT=?";
		Object[] objs = new Object[7];
		objs[0] = pRelativeFilePath;
		objs[1] = pSrcFile;
		objs[2] = pDownState;
		objs[3] = new Date();
		objs[4] = new Date();
		objs[5] = pIstNbr;
		objs[6] = pOwnAct;
		return JDBCHelper.update(partDbconn, sql, objs);
	}
	
	public int updateDownState(ReceiptData pData) throws SQLException, UnsupportedEncodingException{
		String tabName = getTableName(pData.ownAct);
		String sql = "update "+tabName+" set LOCALFILE=?,SRCFILE=?,DOWNSTATE=?,DOWNTIME=?,DOWNINFO=?,LASTMODIFIEDON=?,ROWVERSION=ROWVERSION+1 where URID=?";
		boolean needRowVer = false;
		if(!"2".equals(pData.downState)){//不成功的，需要加上版本号，防止并发
			sql+=" and ROWVERSION=?";
			needRowVer = true;
		}
		Object[] objs;
		if(needRowVer){
			objs = new Object[8];
		}else{
			objs = new Object[7];
		}
		objs[0] = pData.localFile;
		objs[1] = pData.srcFile;
		objs[2] = pData.downState;
		objs[3] = pData.downTime;
		objs[4] = StringHelper.subStringByUtf8Byte(pData.downInfo, 128);
		objs[5] = new Date();
		objs[6] = pData.urid;
		if(needRowVer){
			objs[7] = pData.rowVersion;
		}
		return JDBCHelper.update(partDbconn, sql, objs);
	}
	
	/**更新上传状态
	 * @throws UnsupportedEncodingException */
	public int updateUpState(int pIdx, ReceiptData pData) throws SQLException, UnsupportedEncodingException{
		String tabName = getTabNameByIdx(pIdx);
		String sql = "update "+tabName+" set UPFILE=?,UPSTATE=?,UPTIME=?,UPINFO=?,LASTMODIFIEDON=?,ROWVERSION=ROWVERSION+1 where URID=?";
		boolean needRowVer = false;
		if(!"2".equals(pData.upState)){//不成功的，需要加上版本号，防止并发
			sql+=" and ROWVERSION=?";
			needRowVer = true;
		}
		Object[] objs;
		if(needRowVer){
			objs = new Object[7];
		}else{
			objs = new Object[6];
		}
		objs[0] = pData.upFile;
		objs[1] = pData.upState;
		objs[2] = pData.upTime;
		objs[3] = StringHelper.subStringByUtf8Byte(pData.upInfo, 128);
		objs[4] = new Date();
		objs[5] = pData.urid;
		if(needRowVer){
			objs[6] = pData.rowVersion;
		}
		return JDBCHelper.update(partDbconn, sql, objs);
	}
	
	public int updatePushSuccess(int pIdx, ArrayList<Long> pUridList, Date pPushTime, String pPushInfo) throws UnsupportedEncodingException, SQLException{
		String tabName = getTabNameByIdx(pIdx);
		String sql = "update "+tabName+" set PUSHSTATE='2',PUSHTIME=?,PUSHINFO=?,LASTMODIFIEDON=?,ROWVERSION=ROWVERSION+1 where URID in(" + StringHelper.joinLongArray(",", pUridList) + ")";
		Object[] objs = new Object[3];
		objs[0] = pPushTime;
		objs[1] = StringHelper.subStringByUtf8Byte(pPushInfo, 128);
		objs[2] = new Date();
		return JDBCHelper.update(partDbconn, sql, objs);
	}
	
	/**更新推送状态
	 * @throws UnsupportedEncodingException */
	public int updatePushState(int pIdx, ReceiptData pData) throws SQLException, UnsupportedEncodingException{
		String tabName = getTabNameByIdx(pIdx);
		String sql = "update "+tabName+" set PUSHSTATE=?,PUSHTIME=?,PUSHINFO=?,LASTMODIFIEDON=?,ROWVERSION=ROWVERSION+1 where URID=?";
		boolean needRowVer = false;
		if(!"2".equals(pData.pushState)){//不成功的，需要加上版本号，防止并发
			sql+=" and ROWVERSION=?";
			needRowVer = true;
		}
		Object[] objs;
		if(needRowVer){
			objs = new Object[6];
		}else{
			objs = new Object[5];
		}
		objs[0] = pData.pushState;
		objs[1] = pData.pushTime;
		objs[2] = StringHelper.subStringByUtf8Byte(pData.pushInfo, 128);
		objs[3] = new Date();
		objs[4] = pData.urid;
		if(needRowVer){
			objs[5] = pData.rowVersion;
		}
		return JDBCHelper.update(partDbconn, sql, objs);
	}
}
